/*
 * PlayerManagerPanel.java
 *
 * Created on October 9, 2008, 11:26 AM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.bjj.utils.Utils;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.Timer;
import org.bff.slimserver.SlimPlayer;
import org.bff.slimserver.events.PlayerChangeEvent;
import org.bff.slimserver.events.PlayerChangeListener;
import org.bff.slimserver.events.SleepChangeEvent;
import org.bff.slimserver.events.SleepChangeListener;
import org.bff.slimserver.exception.SlimConnectionException;

/**
 *
 * @author  bfindeisen
 */
public class PlayerManagerPanelMini
        extends BJJPanel
        implements PlayerChangeListener, SleepChangeListener {

    private JukeboxProperties jp;
    private DefaultComboBoxModel playerModel;
    private Timer sleepTimer;
    private long sleepTime;

    /** Creates new form PlayerManagerPanel */
    public PlayerManagerPanelMini() {
        jp = JukeboxProperties.getInstance();
        loadPlayers();

        initComponents();

        cbPlayers.setSelectedItem(jp.getPlayer());

        checkPlayers();

        lblSleep.setVisible(false);

        sleepTimer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                lblSleep.setText("Sleep in " + Utils.formatTime(getSleepTime()));
                setSleepTime(getSleepTime() - 1);
            }
        });

        jp.getSlimMonitor().addPlayerChangeListener(this);
        jp.getSlimMonitor().addSleepChangeListener(this);
    }

    private void checkPlayers() {
        SlimPlayer player = (SlimPlayer) playerModel.getSelectedItem();

        if (player.equals(jp.getPlayer())) {
            btnSet.setEnabled(false);
        } else {
            btnSet.setEnabled(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        cbPlayers = new javax.swing.JComboBox();
        btnSet = new BJJButton();
        lblSleep = new javax.swing.JLabel();
        btnManage = new BJJButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Player Settings"));
        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Player:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
        add(jLabel1, gridBagConstraints);

        cbPlayers.setModel(playerModel);
        cbPlayers.setRenderer(new PlayerRenderer());
        cbPlayers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPlayersActionPerformed(evt);
            }
        });
        add(cbPlayers, new java.awt.GridBagConstraints());

        btnSet.setText("Set");
        btnSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        add(btnSet, gridBagConstraints);

        lblSleep.setText("Sleep in 00:00:00");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
        add(lblSleep, gridBagConstraints);

        btnManage.setText("Manager Players");
        btnManage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnManageActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        add(btnManage, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

private void btnSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetActionPerformed
    SlimPlayer player = (SlimPlayer) cbPlayers.getSelectedItem();
    try {
            jp.setPlayer(player);//GEN-LAST:event_btnSetActionPerformed
        } catch (Exception ex) {
            Logger.getLogger(PlayerManagerPanelMini.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
private void cbPlayersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPlayersActionPerformed
    checkPlayers();
}//GEN-LAST:event_cbPlayersActionPerformed

private void btnManageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnManageActionPerformed
    PlayerManagerDialog pmd = new PlayerManagerDialog(Utils.getParentComponent(this), true);

    pmd.pack();
    Utils.centerInParent(pmd, Utils.getParentComponent(this));
    pmd.setVisible(true);
}//GEN-LAST:event_btnManageActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnManage;
    private javax.swing.JButton btnSet;
    private javax.swing.JComboBox cbPlayers;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblSleep;
    // End of variables declaration//GEN-END:variables

    private void loadPlayers() {
        playerModel = new DefaultComboBoxModel();
        try {
            List<SlimPlayer> playerList = new ArrayList<SlimPlayer>(jp.getSlimServer().getSlimPlayers());
            for (SlimPlayer player : playerList) {
                playerModel.addElement(player);
            }

            playerModel.setSelectedItem(jp.getPlayer());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PlayerManagerPanelMini.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void playerChanged(PlayerChangeEvent event) {
        switch (event.getId()) {
            case PlayerChangeEvent.PLAYER_ADDED:
            case PlayerChangeEvent.PLAYER_DELETED:
                loadPlayers();
                cbPlayers.setModel(playerModel);
                break;
        }
    }

    @Override
    public void sleepTimeChanged(SleepChangeEvent event) {
//        if (event.getPlayer().equals((SlimPlayer) playerModel.getSelectedItem())) {
//            if (event.getSleepTime() > 0) {
//                if (!lblSleep.isVisible()) {
//                    lblSleep.setVisible(true);
//                }
//
//                setSleepTime(event.getSleepTime());
//                if (!sleepTimer.isRunning()) {
//                    sleepTimer.start();
//                }
//            } else {
//                lblSleep.setVisible(false);
//            }
//        }
    }

    /**
     * @return the sleepTime
     */
    public long getSleepTime() {
        return sleepTime;
    }

    /**
     * @param sleepTime the sleepTime to set
     */
    public void setSleepTime(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    private class PlayerRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof SlimPlayer) {
                SlimPlayer player = (SlimPlayer) value;
                try {
                    setText(player.getName() == null ? player.getId() : player.getName());
                } catch (SlimConnectionException ex) {
                    Logger.getLogger(PlayerManagerPanelMini.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }

            return this;
        }
    }
}
